<?php

require_once("constraint.php");

class EKElement {
	
	static function getElementAttributeById($id, $elementClass, $attrName){
		$node = EKElement::getElementXmlNode($id, $elementClass);
		return $node->getAttribute($attrName);
	}
	
	static function getElementValueById($id, $elementClass, $attrName){
		$node = EKElement::getElementXmlNode($id, $elementClass);
		return $node->getElementsByTagName($attrName)->item(0)->nodeValue;
	}
	
	static function testHaveRating($id, $elementClass)
	{
		$node = EKElement::getElementXmlNode($id, $elementClass);
		if($node->getElementsByTagName("ratingId")->length > 0){
			return true;
		}
		else return false;
	}
	
	protected static function deleteRefIdById($parentId, $refId, $parentElementClass, $refAttrName)
	{
		$xmlfile = SERVERLOCATION."data/".$parentElementClass.".xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$node = $doc->getElementById($parentId);
		
		$refIdSet = $doc->getElementsByTagName($refAttrName);
		$i = 0;
		 
		while ($rId = $refIdSet->item($i)) {
		    if($rId->nodeValue == $refId){
		    	#echo $refAttrName.": ".$rId->nodeValue." ".$refId;
		      	$node->removeChild($rId);
		    }
		    $i++;
		}
		$doc->save($xmlfile);
	}
	
	protected static function getElementXmlNode($id, $elementClass){
		$xmlfile = SERVERLOCATION."data/".$elementClass.".xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$node = $doc->getElementById($id);
		return $node;
	}
	
	# append child node to $parent with the text value of $value and name of $name
	protected static function giveNewTextNode($xmlDoc, $name, $value, $parent)
	{
		$node = $xmlDoc->createElement($name);
   		$nodeText = $xmlDoc->createTextNode($value);
   		$node->appendChild($nodeText);
   		$parent->appendChild($node);
   		$blankNode = $xmlDoc->createTextNode("\n");
   		$parent->appendChild($blankNode);
	}
	
	protected static function isAdminPass($action, $state)
	{
		if( $action == "deny" && $state == "pending")
			return true;
		if( $action == "approve" && $state == "pending" )
			return true;
		if( $action == "disapprove" && $state == "approved" )
			return true;
		return false;
	}
	
	
}
?>